package com.iwish.server.model;

import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;

import com.iwish.server.storage.model.Obj;
import com.iwish.server.util.Base64Util;
import com.iwish.server.util.DateUtil;
import com.iwish.server.util.RequestCode;
import com.iwish.server.util.Separator;

public class ActCommentService extends ServiceTemplate{
	
	Logger logger = Logger.getLogger(ActCommentService.class);
	private String aid;
	private String uid;
	private String comment_detail;
	
	public ActCommentService()
	{
		
	}

	public ActCommentService(String aid, String uid, String comment_detail)
	{
		this.aid = aid;
		this.uid = uid;
		this.comment_detail = comment_detail;
	}
	
	public boolean post(IoSession session)
	{
		Obj.Activity.comment.Builder comment = Obj.Activity.comment.newBuilder();
		comment.setUid(uid);
		comment.setCreateDate(DateUtil.getFullDate());
		comment.setInfo(comment_detail);
		
		Object a = get(aid, Obj.Activity.class , 30);
		if(! (a instanceof Obj.Activity))
		{
			logger.info("ActCommentService - can not find the activity with aid");
			String result = Base64Util.encode(RequestCode.ACT_COMMENT_RESULT.getBytes())
					+ Separator.F_SEPARATOR
					+ Base64Util.encode(RequestCode.FAIL.getBytes())
					+ Separator.F_SEPARATOR
					+ "No Such Activity"
					+ Separator.F_SEPARATOR;
			session.write(result);
			return false;
		}
		Obj.Activity act = (Obj.Activity) a;
		act.toBuilder().addComments(comment);
		
		set(Obj.Activity.class, act.getAid(), act.toBuilder().build());
		
		String result = Base64Util.encode(RequestCode.ACT_COMMENT_RESULT.getBytes())
				+ Separator.F_SEPARATOR
				+ Base64Util.encode(RequestCode.SUCCESS.getBytes())
				+ Separator.F_SEPARATOR
				+ Separator.F_SEPARATOR;
		session.write(result);
		
		return true;
	}
}
